//package com.snow.inteceptor;
//
//import com.snow.constant.Types;
//import com.snow.constant.Webconst;
//import com.snow.pojo.model.OptionsDomain;
//import com.snow.pojo.model.UserDomain;
//import com.snow.service.options.OptionsService;
//import com.snow.service.user.UserService;
//import com.snow.utils.*;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.web.servlet.HandlerInterceptor;
//import org.springframework.web.servlet.ModelAndView;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.util.HashMap;
//import java.util.List;
//
///**
// * @Description:
// * 拦截器
// * @author: snow
// * @Date: 2020-01-19
// **/
////@Component
//public class BaseInterceptor implements HandlerInterceptor {
//    private static final Logger LOGGE = LoggerFactory.getLogger(BaseInterceptor.class);
//    private static final String USERAGENT = "user-agent";
//    @Autowired
//    private  UserService userService;
//    @Autowired
//    private Commons commons;
//    @Autowired
//    private AdminCommons adminCommons;
//    @Autowired
//    private  OptionsService optionsService;
//
//    private MapCache mapCache = MapCache.single();
//
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        /**
//         *URI，通一资源标志符(Uniform Resource Identifier， URI)，
//         *表示的是web上每一种可用的资源，如 HTML文档、图像、视频片段、
//         * 程序等都由一个URI进行定位的
//         */
//        String uri = request.getRequestURI();
//
//        LOGGE.info("UserAgent: {}", request.getHeader(USERAGENT));
//        LOGGE.info("用户访问地址: {}, 来路地址: {}", uri, IPKit.getIpAddrByRequest(request));
//
//        LOGGE.info("Enter preHandle Method");
//        System.out.println("Enter PreHadle Method");
//
//        //请求拦截处理
//        //2、利用TalesUtils获取登录的用户对象
//        UserDomain user = TaleUtils.getLoginUser(request);
//        if (null == user) {
//            //3、如果通过session获得user,那么根据UID，从数据库中验证是否有权限，主要是用于不记住我时的网站缓存
//            Integer uid = TaleUtils.getCookieUid(request);
//            if (null != uid) {
//                //这里还是有安全隐患,cookie是可以伪造的
//                user = userService.getUserInfoById(uid);
//                request.getSession().setAttribute(Webconst.LOGIN_SESSION_KEY, user);
//            }
//        }
//        //4、当登陆者无权限时，拦截的访问资源以及重定向到登录页面
//        if (uri.startsWith("/admin") && !uri.startsWith("/admin/login") && null == user
//                && !uri.startsWith("/admin/css") && !uri.startsWith("/admin/images")
//                && !uri.startsWith("/admin/js") && !uri.startsWith("/admin/plugins")
//                && !uri.startsWith("/admin/editormd")) {
//            response.sendRedirect(request.getContextPath() + "/admin/login");
//            return false;
//        }
//        //5、设置get请求的token
//        if (request.getMethod().equals("GET")) {
//            String csrf_token = UUID.UU64();
//            // 默认存储30分钟
//            mapCache.setHashCache(Types.CSRF_TOKEN.getType(), csrf_token, uri, 30 * 60);
//            request.setAttribute("_csrf_token", csrf_token);
//        }
//        LOGGE.info("Exit  preHandle return true");
//        return true;
//    }
//
//
//
//    @Override
//    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        LOGGE.debug("Enter postHandle Method");
//        OptionsDomain sr = optionsService.getOptionsByName("site_record");
//        request.setAttribute("options",sr);
//        request.setAttribute("commons",commons);
//        request.setAttribute("adminCommons",adminCommons);
//        initSiteConfig(request);
//        LOGGE.debug("Exit postHandle Method");
//    }
//
//    private void initSiteConfig(HttpServletRequest request){
//        if(Webconst.initConfig.isEmpty()){
//            List<OptionsDomain> allOptions = optionsService.getAllOptions();
//            HashMap<String, String> temp = new HashMap<>();
//            allOptions.forEach(option->{
//                temp.put(option.getName(),option.getValue());
//            });
//            Webconst.initConfig=temp;
//
//        }
//    }
//
//    @Override
//    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//
//    }
//}
